package org.duiduo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置
 *
 * @author liangze
 */
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(30);
        // 超时时间
        executor.setKeepAliveSeconds(2000);
        // 最大线程数
        executor.setMaxPoolSize(120);
        // 队列数
        executor.setQueueCapacity(600);
        // 拒绝策略 CallerRunsPolicy 如果子线程忙的不行 主线程就上
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

}
